<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Redis;

/**
 * App\Enterprise
 *
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Enterprise onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Enterprise withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Enterprise withoutTrashed()
 * @mixin \Eloquent
 * @property int $id
 * @property string $contact 联系方式
 * @property string $contact_person 联系人
 * @property string $company_name 企业名称
 * @property string $Unified_social_credit_code 统一社会信用代码
 * @property int $business_license_img_id 营业执照的图片
 * @property int $status 状态：0关闭，1开启
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereBusinessLicenseImgId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereCompanyName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereContact($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereContactPerson($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereUnifiedSocialCreditCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereUpdatedAt($value)
 * @property string|null $legal_person 法人
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereLegalPerson($value)
 * @property string|null $end_time 到期时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereEndTime($value)
 * @property string $name 商家名称
 * @property string|null $register_phone 处方平台账号
 * @property string|null $register_password 处方平台密码
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereRegisterPassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereRegisterPhone($value)
 * @property int $login_type 账号类型 1：药店 2：药企
 * @property string|null $check_doctor_name 药店审核医师
 * @property string|null $review_doctor_name 药店复核医师
 * @property string|null $arrange_doctor_name 药店复核医师
 * @property int $drug_store_id 药店id
 * @property string|null $drug_store_name 药店名称
 * @property string|null $app_key 药店的key
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereAppKey($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereArrangeDoctorName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereCheckDoctorName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereDrugStoreId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereDrugStoreName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereLoginType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Enterprise whereReviewDoctorName($value)
 */
class Enterprise extends Model
{
    protected $table = "enterprise";
    use SoftDeletes;

    public static function getEnterpriseName($enterpriseId){
        $data=self::whereId($enterpriseId)->first();
        return $data ? $data->company_name : '无数据';
    }

    /**
     * 获取企业的店铺数组
     * @param $enterpriseId
     * @return array
     */
    public static function getEnterpriseMerchantIds($enterpriseId){
        $key='ent.mer.ids'.$enterpriseId;
        $data=Redis::get($key);
        if ($data)
            return (array)json_decode($data);

        $data=Merchant::whereEnterpriseId($enterpriseId)->pluck('id')->toArray();
        if (count($data) > 0){
            Redis::set($key,json_encode($data),'EX','3600');
            return $data;
        }
        return $data;
    }
    /**
     * 判断该企业是否有指定店铺
     * @param $enterpriseId
     * @param $merchantId
     * @return bool
     */
    public static function isEnterpriseMerchant($enterpriseId,$merchantId){
        if ($merchantId==42)
            return true;
        $key='ent.mer.'.$enterpriseId.'.'.$merchantId;
        $data=Redis::get($key);
        if ($data)
            return true;
        $data=Merchant::whereId($merchantId)->whereEnterpriseId($enterpriseId)->first();
        if ($data){
            Redis::set($key,1,'EX','86400');
            return true;
        }
        return false;
    }

}
